package org.jw.wishlist.config;

import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * 部署相关的配置
 *
 * @author Jiawei Zhao
 */
@Configuration(proxyBeanMethods = false)
public class DeploymentAutoConfiguration {

  @Profile("!dev")
  @Bean
  public ApplicationPidFileWriter applicationPidFileWriter() {
    ApplicationPidFileWriter writer = new ApplicationPidFileWriter();
    /*
     * ApplicationPreparedEvent 不起作用
     * https://stackoverflow.com/questions/45845970/listener-not-receiving-applicationpreparedevent
     *
     * 因为ApplicationPidFileWriter内部机制，只能在以下三个事件选一个
     * - ApplicationEnvironmentPreparedEvent
     * - ApplicationPreparedEvent
     * - ApplicationReadyEvent
     */
    writer.setTriggerEventType(ApplicationReadyEvent.class);
    return writer;
  }

  @Profile("!dev")
  @Bean
  public ApplicationPortFileWriter applicationPortFileWriter() {
    return new ApplicationPortFileWriter();
  }

}
